package com.test.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.common.Result;
import com.test.mapper.UserMapper;
import com.test.pojo.User;
import com.test.service.impl.UserService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.jws.soap.SOAPBinding;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 宋建华
 * @return
 * @date 2023-12-26
 */
// 类加载 解决跨域问题 比如你端口是7000想访问8080
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/add")
    //新增用户信息
    //@RequestBody User user) 前端的user
    public Result save (@RequestBody User user){
        //穿过UserService方法

        try {
            userService.insertUser(user);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException){
                return Result.error("数据库错误");
            }else {
                return Result.error("系统错误");
            }
        }
        return Result.success();

    }

    @PutMapping("/update")
    public Result updateUser(@RequestBody User user){
        userService.updateUser(user);
        return Result.success("成功");
    }
    @DeleteMapping("/delete/{id}")
    public Result deleteUser(@PathVariable Integer id){
        userService.deleteUser(id);
        return Result.success("成功");
    }
    @DeleteMapping("/deletes")
    public Result deleteUsers(@RequestBody List<Integer> ids){
        userService.deleteUsers(ids);
        return Result.success("批量删除成功");
    }
    @GetMapping("/selectAll")
    public Result selectAll (){
        List<User>selectAll = userService.selectAll();
        return Result.success(selectAll);
    }
    @GetMapping("/selectAll/{id}")
    public Result selectAllById(@PathVariable Integer id){
        User user =userService.selectAllId(id);
        return Result.success(user);
    }
    @GetMapping("/selectList/{name}")
    public Result selectListName(@PathVariable String name){
        List<User> userList = userService.selectListName(name);
        return Result.success(userList);
    }
    @GetMapping("/selectByMore")
    public Result selectByMore(@RequestParam String name,@RequestParam Integer id){
        List<User> selectByMore = userService.selectByMore(name,id);
        return Result.success(selectByMore);
    }
    @GetMapping("/userByMo")
    public Result selectByMo(@RequestParam String username,@RequestParam String name){
        List<User> user = userService.selectByMo(username,name);
        return Result.success(user);
    }
    // 分页待会回家看下

}
